package com.fuqi.android.zhongcai.business.shuig.view.activity;

import android.util.Log;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.bean.Question;
import com.fuqi.android.zhongcai.base.request.result.bean.VisitBean;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.business.shuig.request.api.VisitApi;
import com.fuqi.android.zhongcai.business.shuig.request.api.VisitInfoApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.VisitInfoResult;
import com.fuqi.android.zhongcai.business.shuig.request.result.VisitResult;
import com.fuqi.android.zhongcai.business.shuig.view.adapter.AnswerAdapter;
import com.fuqi.android.zhongcai.business.shuig.view.adapter.AnswerQuestion2Adapter;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class HuiFangShowActivity extends BaseActivity {
    @Bind(R.id.gv_visitType)
    GridView mGvVisitType;
    @Bind(R.id.gv_visitContent)
    GridView mGvVisitContent;
    @Bind(R.id.lv_questionList)
    ListView mLvQuestion;
    @Bind(R.id.et_remark)
    TextView mEtRemark;

    AnswerAdapter mTypeAdapter,mContentAdapter;
    AnswerQuestion2Adapter mQuestionAdapter;
    List<VisitBean> typeData = new ArrayList<VisitBean>();
    List<VisitBean> contentData = new ArrayList<VisitBean>();
    List<VisitBean> questionData = new ArrayList<VisitBean>();

//    String sVisitContent,sVisitType,sQuestionList;

    String sTypeData,sContentData;
    List<Question> lsQues;

    ARResponse r;

    Response.Listener<VisitResult> respListener = new Response.Listener<VisitResult>() {

        @Override
        public void onResponse(VisitResult response) {
            if (HandleCode.requestSuccess()) {
                typeData = response.getVisitTypes();
                contentData = response.getVisitContents();
                questionData = response.getQuestions();

                mTypeAdapter.setDate(typeData);
                mContentAdapter.setDate(contentData);
                mQuestionAdapter.setDate(questionData);
                Log.i("juner", "juner VisitResult response : " + GsonHelper.writeValue(response));
                RequestManager.getInstance().call(new VisitInfoApi(
                        new VisitInfoApi.VisitInfoParams(r.getValue("ORDER_ID")), resphListener, errorListener));
            } else {
            }
        }
    };

    Response.Listener<VisitInfoResult> resphListener = new Response.Listener<VisitInfoResult>() {

        @Override
        public void onResponse(VisitInfoResult response) {
            if (HandleCode.requestSuccess()) {
                sTypeData = response.getVisitType();
                sContentData = response.getVisitContent();
                lsQues = response.getQuestionList();
                mEtRemark.setText(response.getRemark());

                for (int i = 0;i < typeData.size();i++){
                    if(typeData.get(i).getBaseCode().equals(sTypeData))
                        mGvVisitType.setItemChecked(i,true);
                }

                for (int i = 0;i < contentData.size();i++){
                    if(contentData.get(i).getBaseCode().equals(sContentData))
                        mGvVisitContent.setItemChecked(i,true);
                }

                mQuestionAdapter.setQuestions(lsQues);
                Log.i("juner", "juner VisitInfoResult response : " + GsonHelper.writeValue(response));
            } else {
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public void initView() {
        mGvVisitType.setEnabled(false);
        mGvVisitContent.setEnabled(false);
        mEtRemark.setEnabled(false);
    }
    @Override
    public void initListener() {
    }

    @Override
    public void initData() {
        titleBar.setTitle("回访查看");
        r = new ARResponse(getIntent().getStringExtra("response"));

        mTypeAdapter = new AnswerAdapter(this, typeData);
        mContentAdapter = new AnswerAdapter(this, contentData);
        mQuestionAdapter = new AnswerQuestion2Adapter(this, questionData);

        RequestManager.getInstance().call(new VisitApi(
                new VisitApi.VisitParams(), respListener, errorListener));

//        mTypeAdapter.setDate(typeData);

        mGvVisitType.setAdapter(mTypeAdapter);
        mGvVisitContent.setAdapter(mContentAdapter);
        mLvQuestion.setAdapter(mQuestionAdapter);
    }

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_huifang_show;
    }


}
